//
//  NSString+Validate.m
//  Marshmallow
//
//  Created by tangkexiong on 16/4/18.
//  Copyright © 2016年 netmoon. All rights reserved.
//

#import "NSString+Validate.h"

@implementation NSString (Validate)

- (NSString *)isVaildInput {
    if (self.length == 0) {
        return @"不能为空";
    } else {
        return nil;
    }
}

- (NSString *)isValidNickName {
    if (self.length == 0) {
        return @"昵称不能为空";
    } else {
        NSString * regex = @"^[a-zA-Z0-9\\u4E00-\\u9FA5]{1,20}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        BOOL isMatch = [pred evaluateWithObject:self];
        if (isMatch) {
            return nil;
        } else {
            return @"昵称不合法";
        }
    }
}

- (NSString *)isValidPhone {
    if (self.length == 0) {
        return @"手机号不能为空";
    } else {
        NSString * regex = @"^1\\d{10}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        BOOL isMatch = [pred evaluateWithObject:self];
        if (isMatch) {
            return nil;
        } else {
            return @"手机号不合法";
        }
    }
}

- (NSString *)isValidCode {
    if (self.length == 0) {
        return @"验证码不能为空";
    } else {
        return nil;
    }
}

- (NSString *)isValidPassword {
    if (self.length == 0) {
        return @"密码不能为空";
    } else {
        NSString * regex = @"^[A-Za-z0-9]{6,20}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        BOOL isMatch = [pred evaluateWithObject:self];
        if (isMatch) {
            return nil;
        } else {
            return @"密码不合法";
        }
    }
}

- (NSString *)isEqualLastPassword:(NSString *)lastPwd {
    if (self.length == 0) {
        return @"确认密码不能为空";
    } else {
        if ([self isEqualToString:lastPwd]) {
            return nil;
        } else {
            return @"确认密码跟密码不一致";
        }
    }
}

- (NSString *)isValidFeedback {
    if (self.length == 0) {
        return @"反馈内容不能为空";
    } else {
        if (self.length > 200) {
            return @"反馈内容最多200字";
        } else {
            return nil;
        }
    }
}

@end
